package lt.marsrutai.mobile.android.model.enums;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime implements Serializable {

	private static final long serialVersionUID = -7719380832256477102L;

    private String originalFormat;
	private long mills;
	private String fullFormat;
	private String date;
	private String time;
	private String hours;
	private String minutes;
	private String year;
	private String month;
	private String day;
	private Calendar calendar;

	public DateTime() {
		calendar = Calendar.getInstance();
	}

	public DateTime(String originalFormat) {
		this.originalFormat = originalFormat;
		mills = Long.parseLong(originalFormat.substring(6, originalFormat
				.indexOf("+")));
		formatData(mills);
	}

	private void formatData(long millis) {
		SimpleDateFormat frm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date dateFrom = new Date(mills);

		calendar = Calendar.getInstance();
		calendar.setTime(dateFrom);

		fullFormat = frm.format(dateFrom);
		date = fullFormat.substring(0, 10);
		year = date.substring(0, 4);
		month = date.substring(5, 7);
		day = date.substring(8, 10);

		time = fullFormat.substring(11, 16);
		hours = time.substring(0, 2);
		minutes = time.substring(3, 5);
	}

	public DateTime(long mills) {
		this.mills = mills;
		formatData(mills);
	}

	public long getMills() {
		return mills;
	}

	public String getOriginalFormat() {
		return originalFormat;
	}

	public String getFullFormnat() {
		return fullFormat;
	}

	public String getDate() {
		return date;
	}

	public String getTime() {
		return time;
	}

	public String getHours() {
		return hours;
	}

	public String getMinutes() {
		return minutes;
	}

	public String getYear() {
		return year;
	}

	public String getMonth() {
		return month;
	}

	public String getDay() {
		return day;
	}

	/**
	 * SUNDAY - 0
	 * MONDAY - 1
	 * ...
	 * SATURDAY - 6
	 * @return
	 */
	public int getWeekDay() {
		return calendar.get(Calendar.DAY_OF_WEEK) - 1;
	}

	public int getCurrentYear() {
		return calendar.get(Calendar.YEAR);
	}

	public int getCurrentMonth() {
		return calendar.get(Calendar.MONTH);
	}

	public int getCurrentDay() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public int getCurrentHour() {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public int getCurrentMinute() {
		return calendar.get(Calendar.MINUTE);
	}

	public Calendar getCalendar() {
	    return calendar;
	}

	public int minutesUntil(Calendar that) {
	    return (that.get(Calendar.HOUR_OF_DAY) - getCalendar().get(Calendar.HOUR_OF_DAY)) * 60 + (that.get(Calendar.MINUTE) - getCalendar().get(Calendar.MINUTE));
	}

	public int compareTimeTo(DateTime that) {
	    final int BEFORE = -1;
	    final int EQUAL = 0;
	    final int AFTER = 1;

	    if (this == that) {
	        return EQUAL;
	    }

	    if (getCalendar().get(Calendar.HOUR_OF_DAY) < that.getCalendar().get(Calendar.HOUR_OF_DAY)) {
	        return BEFORE;
	    }
	    else if (getCalendar().get(Calendar.HOUR_OF_DAY) > that.getCalendar().get(Calendar.HOUR_OF_DAY)) {
	        return AFTER;
	    }
	    else if (getCalendar().get(Calendar.MINUTE) < that.getCalendar().get(Calendar.MINUTE)) {
            return BEFORE;
        }
	    else if (getCalendar().get(Calendar.MINUTE) > that.getCalendar().get(Calendar.MINUTE)) {
            return AFTER;
        }
	    else {
	        return EQUAL;
	    }
	}
}
